package Chapter19.homework;

import java.io.*;
import java.util.Properties;

/**
 * @author 杨森
 * @date 2022年11月05日 11:15
 */
public class Homework03 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String filePath="src\\dog.properties";
        Properties properties = new Properties();
        properties.load(new FileReader(filePath));
        String name=properties.get("name")+"";
        Integer age= Integer.parseInt(properties.get("age")+"");
        String color = (String) properties.get("color")+"";

        Dog dog = new Dog(name,age,color);
        System.out.println(dog);


        String serFilePath="e:\\dog.dat";
        ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream(serFilePath));
        oos.writeObject(dog);
        oos.close();
        System.out.println("序列化完成");

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFilePath));
        Dog dog1=(Dog)ois.readObject();
        System.out.println(dog1);
        ois.close();
    }
}

class Dog implements Serializable{
    private String name;
    private int age;
    private String color;

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Dog{");
        sb.append("name='").append(name).append('\'');
        sb.append(", age=").append(age);
        sb.append(", color='").append(color).append('\'');
        sb.append('}');
        return sb.toString();
    }
}